<template>
  <div class="room-manage">
    <el-card>
      <h3>
        房间管理
        <!-- <el-button type="primary" icon="el-icon-plus" size="small" circle></el-button> -->
        <el-select v-model="filterType" placeholder="选择房间类型" style="float: right;">
          <el-option label="房间类型" value=""></el-option>
          <el-option label="经济间" value="经济"></el-option>
          <el-option label="标准间" value="标准"></el-option>
          <el-option label="商务间" value="商务"></el-option>
          <el-option label="豪华间" value="豪华"></el-option>
          <el-option label="家庭房" value="家庭房"></el-option>
          <el-option label="景观房" value="景观房"></el-option>
          <el-option label="豪华套房" value="豪华套房"></el-option>
          <el-option label="蜜月套房" value="蜜月套房"></el-option>
          <el-option label="复式公寓" value="复式公寓"></el-option>
          <el-option label="水疗房" value="水疗房"></el-option>
          <!-- 添加更多房间类型选项 -->
        </el-select>
        <el-select v-model="filterStatus" placeholder="选择房间状态" style="float: right;">
          <el-option label="房间状态" value=""></el-option>
          <el-option label="空闲" value="空闲"></el-option>
          <el-option label="已预订" value="已预订"></el-option>
          <el-option label="已入住" value="已入住"></el-option>
          <el-option label="维修中" value="维修中"></el-option>
          <!-- 添加更多房间类型选项 -->
        </el-select>
      </h3>
      <el-table style="width: 100%" :data="filteredComments">
        <el-table-column type="selection" width="55"> </el-table-column>
        <el-table-column label="序号" type="index" :index="indexMethod"></el-table-column>
        <el-table-column prop="roomType" label="房间类型"> </el-table-column>
        <el-table-column prop="roomNumber" label="房间号" width="80px"> </el-table-column>
        <el-table-column prop="roomUrl" label="房间图片">
          <template slot-scope="scope">
            <img :src="scope.row.roomUrl" alt="" style="width: 100%;height: 50px;">
          </template>
        </el-table-column>
        <el-table-column prop="description" label="类型说明" width="100px"> </el-table-column>
        <el-table-column prop="typeExplain" label="房间描述" width="300px"></el-table-column>
        <el-table-column prop="price" label="房间价格"> </el-table-column>
        <el-table-column prop="status" label="房间状态"> </el-table-column>
        </el-table-column>
        <el-table-column fixed="right" label="操作" width="100">
          <template slot-scope="scope">
            <el-button @click="editUser(scope.row.roomNumber)" type="primary" size="small" icon="el-icon-edit"
              circle></el-button>
            <!-- <el-button type="danger" size="small" circle icon="el-icon-delete"></el-button> -->
          </template>
        </el-table-column>
      </el-table>

      <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="currentPage"
        :page-sizes="pageSizes" :page-size="pageSize" layout="total, sizes, prev, pager, next, jumper" :total="total">
      </el-pagination>
    </el-card>


    <el-dialog title="请填写房间状态" :visible.sync="dialogFormVisible">
      <el-form :model="form">
        <el-form-item label="房间号" :label-width="formLabelWidth">
          <el-input v-model="form.roomNumber" autocomplete="off" type="number" disabled></el-input>
        </el-form-item>
        <el-form-item label="房间状态" :label-width="formLabelWidth">
          <!-- <el-input v-model="form.logLevel" autocomplete="off" placeholder="请输入操作等级"
            value="this.form.logLevel"></el-input> -->
          <el-select v-model="form.status" placeholder="选择状态">
            <el-option label="空闲" value="1"></el-option>
            <el-option label="已预订" value="2"></el-option>
            <el-option label="已入住" value="3"></el-option>
            <el-option label="维修中" value="4"></el-option>
          </el-select>
          <!-- <el-radio v-model="form.logLevel" label="INFO">INFO</el-radio>
          <el-radio v-model="form.logLevel" label="DEBUG">DEBUG</el-radio>
          <el-radio v-model="form.logLevel" label="WARN">WARN</el-radio>
          <el-radio v-model="form.logLevel" label="ERROR">ERROR</el-radio> -->
        </el-form-item>
        <!-- <el-form-item label="操作者" :label-width="formLabelWidth">
          <el-input v-model="form.logUser" autocomplete="off" placeholder="请输入操作者" value="this.form.logUser"></el-input>
        </el-form-item>
        <el-form-item label="操作内容" :label-width="formLabelWidth">
          <el-input v-model="form.logContent" autocomplete="off" placeholder="请输入操作内容"
            value="this.form.logContent"></el-input>
        </el-form-item> -->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="updateType">更 新</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { getRoomList, getAllRoom, updateRoomStatus } from '@/api/room'

export default {
  name: 'RoomManage',
  data() {
    return {
      currentPage: 1, //当前页
      total: 0, //总条数
      pageSize: 3, //当前页容量
      pageSizes: [],
      filterType: '', // 筛选条件
      filterStatus: '',
      roomList: [],
      dialogFormVisible: false,
      form: {
        roomType: '',
        roomNumber: '',
        status: '',
        description: '',
        typeExplain: ''
      },
      formLabelWidth: '120px',
      status: 0
      // datalist: [
      //   {
      //     roomId: 1,
      //     roomType: '标准单人间',
      //     TypeDetails: '1张2米特大床 70m',
      //     roomPrice: 108,
      //     roomNumber: 6,
      //     description: '经济实惠，简约设计',
      //     roomImg: 'https://dimg04.c-ctrip.com/images/0202r12000a35nd59238C_W_1080_808_R5_D.jpg_.webp'
      //   },
      //   {
      //     roomId: 2,
      //     roomType: '标准双人间',
      //     TypeDetails: '1张2米特大床 70m',
      //     roomPrice: 128,
      //     roomNumber: 2,
      //     description: '适合旅行，舒适惬意'
      //   },
      //   {
      //     roomId: 3,
      //     roomType: '经济单人间',
      //     TypeDetails: '1张2米特大床 70m',
      //     roomPrice: 148,
      //     roomNumber: 5,
      //     description: '经济实惠，简约设计'
      //   },
      //   {
      //     roomId: 4,
      //     roomType: '经济双人间',
      //     TypeDetails: '1张2米特大床 70m',
      //     roomPrice: 168,
      //     roomNumber: 4,
      //     description: '经济实惠，简约设计'
      //   },
      //   {
      //     roomId: 5,
      //     roomType: '商务单人间',
      //     TypeDetails: '1张2米特大床 70m',
      //     roomPrice: 188,
      //     roomNumber: 7,
      //     description: '商务设施，高效率'
      //   },
      //   {
      //     roomId: 6,
      //     roomType: '商务双人间',
      //     TypeDetails: '1张2米特大床 70m',
      //     roomPrice: 208,
      //     roomNumber: 1,
      //     description: '商务设施，高效率'
      //   },
      //   {
      //     roomId: 7,
      //     roomType: '豪华单人间',
      //     TypeDetails: '1张2米特大床 70m',
      //     roomPrice: 308,
      //     roomNumber: 9,
      //     description: '带办公桌，适合出差'
      //   },
      //   {
      //     roomId: 8,
      //     roomType: '豪华套房',
      //     TypeDetails: '1张2米特大床 70m',
      //     roomPrice: 588,
      //     roomNumber: 10,
      //     description: '带私人泳池，豪华享受'
      //   }
      // ],
    }
  },
  computed: {
    filteredComments() {
      return this.roomList.filter(room => {
        return (
          (!this.filterType || room.roomType.includes(this.filterType)) &&
          (!this.filterStatus || room.status.includes(this.filterStatus))
        );
      }).slice((this.currentPage - 1) * this.pageSize, this.currentPage * this.pageSize);
    },
  },
  created() {
    this.fetchData()
    for (let i = 0; i < 5; i++) {
      this.pageSizes.push(this.pageSize * (i + 1))
    }
  },
  watch: {
    filterType() {
      this.currentPage = 1; // 重置到第一页
      // this.fetchData();
      this.fetchData()
    },
    dialogFormVisible(val) {
      if (val === false) {
        this.status = 0
      }
    }
  },
  methods: {
    fetchData() {
      getAllRoom().then((res) => {
        // console.error(res)
        this.roomList = res
        this.total = this.roomList.length
      })
    },

    //切换每页条数容量
    handleSizeChange(val) {
      this.currentPage = 1;
      this.pageSize = val;
      // this.getList();
    },
    //切换当前页
    handleCurrentChange(val) {
      this.currentPage = val;
      // this.getList();
    },
    handleClick(id) {
      console.log(id)
    },
    indexMethod(index) {
      return index + 1 + (this.currentPage - 1) * this.pageSize;
    },
    // filterRooms() {
    //   if (this.filterType) {
    //     return this.roomList.filter(room => room.roomType.includes(this.filterType));
    //   }
    //   return this.roomList;
    // },
    deleteUser(id) {
      this.$confirm('此操作将永久删除该日志, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        console.warn(id)
        deleteLog(id).then(res => {
          this.fetchData()
          this.currentPage = 1
          this.$message({
            type: 'success',
            message: '删除成功!'
          })
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    editUser(id) {
      this.roomList.forEach(item => {
        if (item.roomNumber === id) {
          this.form = item
        }
      })
      console.warn(this.form)
      // this.status = id
      this.dialogFormVisible = true
    },
    updateType() {
      console.error(this.form.roomNumber.toString())
      updateRoomStatus(this.form.roomNumber, this.form.status).then(res => {
        this.$message({
          type: 'success',
          message: '更新成功!'
        })
        this.status = 0
        this.dialogFormVisible = false
        this.fetchData()
      }).catch(err => {
        this.$message({
          showClose: true,
          message: '更新失败' + err.message,
          type: 'error'
        });
      })
    },
  }
}
</script>

<style lang="less" scoped>
.room-manage {
  display: flex;
  justify-content: center;
}

.el-card {
  display: flex;
  flex-direction: column;
  width: 94%;
  margin: 20px 0;

  h3 {
    position: relative;
    height: 35px;

    .el-button {
      position: absolute;
      right: 10px;
      top: 5;
    }
  }

  .el-table {
    width: 100%;
    text-align: center;
  }
}

.el-pagination {
  margin: 10px 0 10px 300px;
}
</style>
